import { Injectable, Type } from '@angular/core';
import { Subject } from 'rxjs';

import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { ModalOptions, NzModalService } from 'ng-zorro-antd/modal';

export interface DefaultModalOptions {
  nzTitle?: string;
  nzOkText?: string | null;
  nzCancelText?: string | null;
  nzCentered?: boolean;
  nzClosable?: boolean;
  nzOkLoading?: boolean;
  nzCancelLoading?: boolean;
  nzOkDisabled?: boolean;
  nzCancelDisabled?: boolean;
  nzKeyboard?: boolean;
  nzMask?: boolean;
  nzMaskClosable?: boolean;
  nzCloseOnNavigation?: boolean;
  nzOkDanger?: boolean;
  nzVisible?: boolean;
  nzWidth?: number;
}

/**
 * 提示框
 */
@Injectable({ providedIn: 'root' })
export class TipsModalService {
  constructor(private modalSrc: NzModalService) {}

  info<T>(content: string): Subject<T> {
    return this.common('信息提示', content, 'info');
  }

  warning<T>(content: string): Subject<T> {
    return this.common('警告提示', content, 'warning');
  }

  success<T>(content: string): Subject<T> {
    return this.common('成功提示', content, 'success');
  }

  fail<T>(content: string): Subject<T> {
    return this.common('失败提示', content, 'error');
  }

  show(component: Type<NzSafeAny>, options: DefaultModalOptions = {}, params: object = {}): Subject<any> {
    const defaultOptions: ModalOptions = {
      nzContent: component,
      nzMaskClosable: false,
      nzCancelText: null,
      nzComponentParams: params
    };

    const mergeOptions = { ...defaultOptions, ...options };
    const modal = this.modalSrc.create(mergeOptions);
    return modal.afterClose;
  }

  private common<T>(title: string, content: string, type: string): Subject<T> {
    // @ts-ignore
    const modal = this.modalSrc[type]({
      nzTitle: title,
      nzContent: content
    });

    return modal.afterClose;
  }
}
